package com.bjesc_b2c.app.appraiset.widget.popupwindow;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.widget.BasePopupWindow;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import master_bean.CarSeriesModelBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/5/9.
 */

public class AdjustmentPop extends BasePopupWindow implements View.OnClickListener {

    private EditText editTextPrice;
    private AdjustmencallBack callBack;
    private String car_Id;
    public AdjustmentPop(Context context,String car_Id,AdjustmencallBack callBack) {
        super(context, null);
        this.callBack = callBack;
        this.car_Id = car_Id;
    }

    @Override
    protected void setAnim(int styleId) {
        super.setAnim(R.style.popupwindow_bottomtotop_anim);
    }

    @Override
    protected void initViews() {
        view = inflater.inflate(R.layout.dialog_price_adjustment,null);
        view.findViewById(R.id.iv_dismiss).setOnClickListener(this);
        view.findViewById(R.id.btn_commit).setOnClickListener(this);
        editTextPrice = (EditText) view.findViewById(R.id.editTextPrice);

    }

    @Override
    protected int setRootViewId() {
        return R.id.lin_root;
    }

    @Override
    public void onClick(View view) {
       switch (view.getId()){
           case R.id.iv_dismiss:
               dismiss();
               break;
           case R.id.btn_commit:
              if (TextUtils.isEmpty(editTextPrice.getText().toString())){
                  Toast.makeText(context,"请填写价格",Toast.LENGTH_SHORT).show();
              }else{
                  editPrice(car_Id,editTextPrice.getText().toString());
              }
               break;
       }
    }
    private void editPrice(String carId, String price) {
        HashMap<String, Object> build = new MapUtil()
                .put("car_id", carId)
                .put("owner_price", price)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, build).create(IApiActivityRequest.class).editPrice(),
                new BaseSubscriber<TokenBean<CarSeriesModelBean>>(context) {
                    @Override
                    public void onSuccess(TokenBean<CarSeriesModelBean> o) {
                        if(callBack!=null){
                            callBack.adjustmenSuccess(o.getData());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }
    public interface AdjustmencallBack{
        void adjustmenSuccess(CarSeriesModelBean cbean);
    }
}
